<template>
  <el-dialog
    title="添加分类"
    :visible.sync="dialogVisibleAdd"
    :before-close="closedDialog"
  >
    <el-form :model="form" ref="ruleForm" label-position="top">
      <el-form-item label="分类名称" :label-width="formLabelWidth">
        <el-input v-model="form.categoryName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="分类" prop="cat_parent">
        <!--级联选择器的使用  -->
        <el-cascader
          :options="categoryData"
          :props="cascaderProps"
          v-model="form.cascaderSelected"
          @change="handleCategoryChange"
          clearable
        ></el-cascader>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closedDialog">取 消</el-button>
      <el-button type="primary" @click="submitDialog">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    dialogVisibleAdd: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        categoryName: '',
        cascaderSelected: []
      },
      formLabelWidth: '120px',
      categoryData: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        checkStrictly: true
      }
    }
  },
  watch: {
    dialogVisibleAdd(val) {
      if (val) {
        this.getCategoryListData()
      }
    }
  },
  methods: {
    handleCategoryChange(options) {
      // console.log(options)
    },
    // 获取级联菜单选择备选数据
    async getCategoryListData() {
      const { data } = await this.$http.get('/categories?type=2')

      // console.log(data)
      if (data.meta.status === 200) {
        this.categoryData = data.data
      } else {
        this.$message.warning(data.meta.msg)
      }
    },
    // 关闭窗口事件
    closedDialog() {
      this.$emit('addCategoryClosed')
      this.form = {
        categoryName: '',
        cascaderSelected: []
      }
    },
    async submitDialog() {
      // 处理 form 数据
      const { categoryName, cascaderSelected } = this.form

      if (categoryName.trim() === '') {
        this.$message.warning('请填写分类名称')
        return
      }

      // 级联选择的选择项，关联添加几级分类
      // 长度 0，添加 一级分类（0），cat_pid = 0
      // 长度 1，添加 二级分类（1），cat_pid = arr[length-1]
      // 长度 2，添加 三级分类（2），cat_pid = arr[length-1]
      const tempForm = {
        cat_pid:
          cascaderSelected.length === 0
            ? 0
            : cascaderSelected[cascaderSelected.length - 1],
        cat_name: categoryName,
        cat_level: cascaderSelected.length
      }

      const { data } = await this.$http.post('categories', tempForm)
      if (data.meta.status === 201) {
        // 添加成功后，关闭对话框，更新 category页 表格数据
        this.$message.success(data.meta.msg)
        this.$emit('addCategoryClosed')
        this.$emit('addCategorySuccess')
      } else {
        this.$message.warning(data.meta.msg)
      }

      this.form = {
        categoryName: '',
        cascaderSelected: []
      }
    }
  }
}
</script>

<style></style>
